#include "StdAfx.h"
#include "Ring.h"
#include "Global.h"
#include "NaviService.h"
CRing::CRing(void)
{
	m_pIMediaControl = NULL;
	m_pIGraph = NULL;
	m_pIEvent = NULL;
	m_pIBasicAudio = NULL;
	m_pISeeking = NULL;
}

CRing::~CRing(void)
{
	Delete();
}

bool CRing::Create(HWND hNotifyWindow,CString sFile)
{
	Delete();

	if(FAILED(CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC,
		IID_IGraphBuilder,(void **)&m_pIGraph)))
	{
		Delete();
		return false;
	}


	if(!QueryInterfaces())
	{
		Delete();
		return false;
	}
	/////////

	if(!RenderFile(sFile))
	{
		Delete();
		return false;
	}
	////////////////////
	if(!SetNotifyWindow(hNotifyWindow))
	{
		Delete();
		return false;
	}
	Pause();
	return true;
}

void CRing::Delete()
{
	if(m_pISeeking)
	{
		m_pISeeking->Release();
		m_pISeeking = NULL;
	}
	if(m_pIBasicAudio)
	{
		m_pIBasicAudio->Release();
		m_pIBasicAudio = NULL;
	}
	if(m_pIEvent)
	{
		m_pIEvent->Release();
		m_pIEvent = NULL;
	}
	if(m_pIMediaControl)
	{
		m_pIMediaControl->Release();
		m_pIMediaControl = NULL;
	}
	if(m_pIGraph)
	{
		m_pIGraph->Release();	
		m_pIGraph = NULL;
	}
}
bool CRing::QueryInterfaces()
{
	if(m_pIGraph)
	{
		HRESULT hr = NOERROR;
		hr &= m_pIGraph->QueryInterface(IID_IMediaControl,(void **)&m_pIMediaControl);
		hr &= m_pIGraph->QueryInterface(IID_IMediaEventEx,(void **)&m_pIEvent);
		hr &= m_pIGraph->QueryInterface(IID_IBasicAudio,(void **)&m_pIBasicAudio);
		hr &= m_pIGraph->QueryInterface(IID_IMediaSeeking,(void **)&m_pISeeking);
		if(m_pISeeking)
		{
			m_pISeeking->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME);
		}
		return SUCCEEDED(hr);
	}
	return false;	
}
bool CRing::RenderFile(CString sFile)
{
	if(m_pIGraph)
	{
		if (SUCCEEDED(m_pIGraph->RenderFile((LPCTSTR)sFile, NULL)))
		{
			return true;
		}
		printf("\nCRing::RenderFile:m_pIGraph");
	}
	return false;
}
bool CRing::SetNotifyWindow(HWND hwndNotify)
{
	if(m_pIGraph)
	{
		if(hwndNotify&&SUCCEEDED(m_pIEvent->SetNotifyWindow((OAHWND)hwndNotify, WM_GRAPHNOTIFY,0)))
			return true;
	}
	return false;
}

bool CRing::Run()
{
	if(m_pIGraph&&m_pIMediaControl)
	{
		if(IsStopped()||IsPaused())
		{
			if(FAILED(m_pIMediaControl->Run()))
				return false;
		}
		return true;
	}
	return false;
}

bool CRing::Pause()
{
	if(m_pIGraph&&m_pIMediaControl)
	{
		if(IsRunning()||IsStopped())
		{
			if(FAILED(m_pIMediaControl->Pause()))
				return false;
		}
		return true;
	}
	return false;
}

bool CRing::Stop()
{
	if(m_pIGraph&&m_pIMediaControl)
	{
		if(!IsStopped())
		{
			if(FAILED(m_pIMediaControl->Stop()))
				return false;
		}
		return true;
	}
	return false;
}
bool CRing::IsRunning()
{
	if(m_pIGraph&&m_pIMediaControl)
	{
		OAFilterState state = State_Stopped;
		if(SUCCEEDED(m_pIMediaControl->GetState(599, &state)))
		{
			return state == State_Running;
		}
	}
	return false;
}

bool CRing::IsStopped()
{
	if(m_pIGraph&&m_pIMediaControl)
	{
		OAFilterState state = State_Stopped;
		if(SUCCEEDED(m_pIMediaControl->GetState(500, &state)))
		{
			return state == State_Stopped;
		}
	}
	return false;
}

bool CRing::IsPaused()
{
	if(m_pIGraph&&m_pIMediaControl)
	{
		OAFilterState state = State_Stopped;
		if(SUCCEEDED(m_pIMediaControl->GetState(500, &state)))
		{
			return state == State_Paused;
		}
	}
	return false;
}
IMediaEventEx * CRing::GetEventHandle(void)
{
	return m_pIEvent;
}
bool CRing::SetCurrentPosition(double inPosition)
{
	if (m_pISeeking)
	{
		__int64 one = 10000000;
		__int64 position = (__int64)(one * inPosition);
		HRESULT hr = m_pISeeking->SetPositions(&position, AM_SEEKING_AbsolutePositioning | AM_SEEKING_SeekToKeyFrame, 
			0, AM_SEEKING_NoPositioning);
		return SUCCEEDED(hr);
	}
	return false;
}


void CRing::StartRing(int index)
{
	CString songName;
	songName.Format(_T("\\windows\\%d.wav"),index);
	PlaySound(songName, NULL, SND_ASYNC);
	return;
	if(index == 110)
	{
		PlaySound(_T("\\windows\\110.wav"), NULL, SND_ASYNC);
		return;
	}
	CString str;
	str.Format(_T("\\windows\\%d.mp3"),index);
	if(IsRunning())
	{
		return;
	}

	if(Create(theApp.m_pMainWnd->m_hWnd,str))
	{
		if(Run())
		{
		}
		else
		{
			printf("\n-----Run Error!----");
		}
	}
	else
	{
		printf("\n-----Ring create error!----");
		Delete();
	}
}
void CRing::RepeatRing()
{
	SetCurrentPosition(0);
	Run();
}
void CRing::StopRing()
{
	TRACE(L"\nStop===================================");
	Stop();
	Delete();	
}

